<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card style="padding: 0 20px;">
          <el-row>
            <el-form ref="dataForm" label-width="130px" size="small">
              <el-row :gutter="20">
                <el-col :span="24">
                  <span>除外的节点id（多个id用逗号分割）：</span>
                  <el-input v-model="dataForm.exceptIds"></el-input>
                </el-col>
                <el-col :span="24">
                  <span>请输入表名：</span>
                  <el-input v-model="dataForm.tableName"></el-input>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="20">
                  <span>请输入bpmn相对路径：</span>
                  <el-input v-model="dataForm.bpmn"></el-input>
                </el-col>
              </el-row>
            </el-form>
          </el-row>
          <!-- <el-row  id="sqlCode" style="margin-top: 20px;">
            <codemirror
              ref="mycode"
              v-model="xmlCode"
              :options="cmOptions"
              class="code">
            </codemirror>
            </codemirror>
          </el-row>
          -->
        </el-card>
      </el-col>
      <el-col :span="12">
        <div>
          <el-card style="margin-bottom: 20px">
            <el-row>
              <el-button type="primary" @click="generateSql">解析流程图</el-button>
            </el-row>
            <el-row id="sqlCode" style="margin-top: 20px;">
              <el-input v-model="sqlCode" :rows="20" type="textarea"></el-input>
              <!--              <codemirror-->
              <!--                ref="mycode2"-->
              <!--                v-model="sqlCode"-->
              <!--                :options="cmOptions2">-->
              <!--              </codemirror>-->
            </el-row>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { analyzeBpmn } from '@/api/activiti/definition'
import { codemirror } from 'vue-codemirror'
import 'codemirror/theme/rubyblue.css'

require('codemirror/mode/xml/xml')
require('codemirror/mode/sql/sql')

export default {
  name: 'Analyze',
  components: {
    codemirror
  },
  data() {
    return {
      dataForm: {
        exceptIds: '',
        tableName: '',
        bpmn: ''
      },
      sqlCode: '',
      cmOptions: {
        value: this.xmlCode,
        lineNumbers: true,
        lineWrapping: true,
        mode: 'xml',
        theme: 'rubyblue',
        gutter: true,
        readOnly: false
      },
      cmOptions2: {
        value: '',
        lineNumbers: true,
        lineWrapping: true,
        mode: 'sql',
        theme: 'rubyblue',
        gutter: true,
        readOnly: true
      }
    }
  },
  created() {

  },
  methods: {
    generateSql() {
      let _this = this
      analyzeBpmn({ bpmn: this.dataForm.bpmn, exceptIds: this.dataForm.exceptIds }).then(res => {
        console.log(res)
        let sqlCode = ''
        sqlCode = sqlCode + `INSERT INTO process_schedule_configure (business_key, node_name, node_value, sort, remark) VALUES ('` + _this.dataForm.tableName + `', '创建业务', 'create', 1, NULL);\n`
        sqlCode = sqlCode + `INSERT INTO process_schedule_configure (business_key, node_name, node_value, sort, remark) VALUES ('` + _this.dataForm.tableName + `', '提交', 'submit', 2, NULL);\n`
        let currentNum = 2
        if (res.data.resultCode === 'ok') {
          let ketList = res.data.keyList
          ketList.forEach(item => {
            if (ketList.indexOf(item) === 0 || ketList.indexOf(item) === ketList.length - 1) return
            currentNum = currentNum + 1
            sqlCode = sqlCode + `INSERT INTO process_schedule_configure (business_key, node_name, node_value, sort, remark) VALUES ('` + _this.dataForm.tableName + `', '`
            let a = item.name
            sqlCode = sqlCode + a + `', '`
            let b = item.key
            sqlCode = sqlCode + b + `', `
            sqlCode = sqlCode + currentNum + `, NULL);\n`
          })
          currentNum = currentNum + 1
          sqlCode = sqlCode + `INSERT INTO process_schedule_configure (business_key, node_name, node_value, sort, remark) VALUES ('` + _this.dataForm.tableName + `', '已完结', 'review', ` +
            currentNum
            + `, NULL);\n`
          console.log(sqlCode)
          _this.sqlCode = sqlCode
        } else {
          _this.$message(res.data.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  width: 100%;
  display: flex;
}
</style>
<style lang="scss">
.el-form-item--medium {
  .el-form-item__content {
    flex: 1;
  }
}
</style>
<style lang="scss">
#xmlCode {
  .CodeMirror {
    overscroll-y: scroll !important;
    height: 500px !important;
  }
}

#sqlCode {
  .CodeMirror {
    overscroll-y: scroll !important;
    height: 500px !important;
  }
}
</style>
